package com.phpxiu.app.view;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.MySelfInfo;
import com.phpxiu.app.model.response.UserGradeModel;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;
import com.phpxiu.app.utils.KKYUtil;

/**
 * 等级
 * Created by WuJinZhou on 2016/6/4.
 */
public class UserGrade extends UIBase implements View.OnClickListener {
    public final static String TAG="UserGrade";
    private final static ForegroundColorSpan SPAN=new ForegroundColorSpan(Color.parseColor("#ff24a8df"));
    private final static ForegroundColorSpan SPAN1=new ForegroundColorSpan(Color.parseColor("#ff24a8df"));
    private ProgressBar lvPro;
    private TextView lvTitle,lvTxt;
    private int currentValue=0;
    private int totalValue=0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_grade);
        findViewById(R.id.top_bar_back_btn).setOnClickListener(this);
        lvPro=(ProgressBar)findViewById(R.id.live_lv_progress_bar);
        lvTxt=(TextView)findViewById(R.id.live_lv_progress_bar_txt);
        lvTitle=(TextView)findViewById(R.id.level_info);
        MySelfInfo.getInstance().getCache(this);
        request();
    }

    /**
     * 初始化数据
     */
    private void request(){
        OKHttpParam param=OKHttpParam.builder(false);
        param.put("uid", MySelfInfo.getInstance().getId());
        OKHttp.post(HttpConfig.API_USER_GRAD, param.jsonParam(), TAG, new OKHttpUIHandler(UserGradeModel.class) {
            @Override
            public void onSuccess(Object model) {
                UserGradeModel m = (UserGradeModel) model;
                KKYUtil.log("用户中心等级：" + m.getResponseStr());
                String currentLev = getString(R.string.ui_level_label);
                SpannableStringBuilder title=new SpannableStringBuilder(currentLev);
                String lev=m.getData().getLvl();
                title.append(lev);
                title.setSpan(SPAN,currentLev.length()-2,title.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                lvTitle.setText(title);
                try {
                    currentValue = Integer.parseInt(m.getData().getExp());
                    totalValue = Integer.parseInt(m.getData().getTotal());
                    String p = currentValue + "";
                    String q = totalValue + "";
                    String t=getString(R.string.ui_level_next_label);
                    String r=getString(R.string.ui_level_exp_label);
                    SpannableStringBuilder exp=new SpannableStringBuilder();
                    exp.clear();
                    exp.append(r).append(p).append(" ").append(t).append(q);
                    exp.setSpan(SPAN, r.length(), r.length() + p.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    exp.setSpan(SPAN1,r.length()+p.length()+t.length()+1,exp.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    lvTxt.setText(exp);
                    lvPro.setMax(totalValue);
                    lvPro.setProgress(currentValue);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErr(String errMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_bar_back_btn:
                onBackPressed();
                break;
            default:
                break;
        }
    }
}
